package com.brillilab.starter.controller.crm;

import com.aliyun.openservices.shade.org.apache.commons.lang3.StringUtils;
import com.aliyun.oss.OSSClient;
import com.brillilab.common.alioss.OssUtils;
import com.brillilab.common.entity.ResponseVo;
import com.brillilab.domain.vo.crm.CrmUsersInfoVo;
import com.brillilab.domain.vo.lab.LabApplyInforVo;
import com.brillilab.domain.vo.lab.LabApplyListVo;
import com.brillilab.domain.vo.lab.LabAuditVo;
import com.brillilab.service.core.lab.ILabApplyService;
import com.brillilab.service.core.system.IDictService;
import com.brillilab.service.logic.crm.CrmUsersLogic;
import com.brillilab.service.logic.lab.LabApplyLogic;
import com.brillilab.service.logic.message.MessageLogic;
import com.brillilab.service.module.aliyun.AliYunSms;
import com.brillilab.starter.constant.StarterConstant;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author zhouxiong
 * @Title: CrmController
 * @ProjectName brill_lab
 * @Description: TODO
 * @date 2019/6/2718:07
 */
@RestController
@RequestMapping("/crm/labAudit")
public class CrmAuditController {
    @Resource
    private ILabApplyService labApplyService;

    @Resource
    private CrmUsersLogic crmUsersLogic;

    @Resource
    private LabApplyLogic labApplyLogic;

    @Resource
    private IDictService dictService;

    @Resource
    private OssUtils ossUtil;

    @Resource
    private AliYunSms aliYunSms;

    @Resource
    private MessageLogic messageLogic;


    /**
     * 实验室申请列表
     *
     * @param token
     * @param labApplyListVo
     * @return
     */
    @PostMapping("/labApplyList")
    public ResponseVo labApplyList(@RequestHeader(name = StarterConstant.TOKEN) String token, @RequestBody LabApplyListVo labApplyListVo) {
        CrmUsersInfoVo crmUsersInfoVo = crmUsersLogic.getCrmUserByToken(token);
        Integer pageNum = labApplyListVo.getPageNum();
        Integer pageSize = labApplyListVo.getPageSize();
        return ResponseVo.success(labApplyService.selectByState(labApplyListVo.getApplyState(), labApplyListVo.getMobile(), pageNum, pageSize));
    }


    /**
     * 获取实验室申请信息
     *
     * @param applyId
     * @param token
     * @return
     */
    @GetMapping("/getApplyInfo")
    public ResponseVo getApplyInfo(Integer applyId, @RequestHeader(name = StarterConstant.TOKEN) String token) {
        Assert.isTrue(StringUtils.isNotEmpty(token), "token不能为空！");
        CrmUsersInfoVo crmUsersInfoVo = crmUsersLogic.getCrmUserByToken(token);
        LabApplyInforVo labApplyInforVo = labApplyService.selectByApplyId(applyId);
        Assert.notNull(labApplyInforVo, "申请信息不存在，请核实！");


        //申请人为PI
        if (labApplyInforVo.getIsOwner().equals(1)) {
            labApplyInforVo.setDutyName("PI");
        } else {
            //申请人非PI，显示对应的职务名称
            Map<Integer, String> userDutyMap = dictService.getUserDutyMap();
            if (userDutyMap != null) {
                labApplyInforVo.setDutyName(userDutyMap.get(labApplyInforVo.getDuty()));
            }
        }
        if (StringUtils.isEmpty(labApplyInforVo.getLabLogo())) {
            //若实验室logo为空,返回默认头像
            labApplyInforVo.setLabLogo("https://labinone-public.oss-cn-hangzhou.aliyuncs.com/image/default/lab_default.png");
        }
        //证件照处理
        if (StringUtils.isNotEmpty(labApplyInforVo.getCardImg())) {
            String[] imgArr = labApplyInforVo.getCardImg().split(",");
            String bucketName = imgArr[0];
            String objectName = imgArr[1];
            OSSClient ossClient = ossUtil.initClient();
            labApplyInforVo.setCardImg(ossUtil.getAccessUrl(ossClient, bucketName, objectName));
        }

        return ResponseVo.success(labApplyInforVo);
    }

    /**
     * 实验室审核
     *
     * @param labAuditVo
     * @param token
     * @return
     */
    @PostMapping("/audit")
    public ResponseVo auditLab(@RequestBody LabAuditVo labAuditVo, @RequestHeader(name = StarterConstant.TOKEN) String token) {
        Assert.isTrue(StringUtils.isNotEmpty(token), "token不能为空！");
        Assert.isTrue(labAuditVo.getApplyId() != null, "请输入申请ID！");
        Assert.isTrue(labAuditVo.getApplyState() != null, "请选择审核状态！");
        if (labAuditVo.getApplyState() == 2 && StringUtils.isEmpty(labAuditVo.getAuditInfo())) {
            return ResponseVo.failure("请输入不通过原因！");
        }
        CrmUsersInfoVo crmUsersInfoVo = crmUsersLogic.getCrmUserByToken(token);
        return labApplyLogic.auditLab(labAuditVo, crmUsersInfoVo);
    }


}
